/*
 * Copyright (C) 2013 47 Degrees, LLC
 * http://47deg.com
 * http://apps.ly
 * hello@47deg.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ly.apps.android.rest.tests;

import android.test.InstrumentationTestCase;
import ly.apps.android.rest.converters.BodyConverter;
import ly.apps.android.rest.converters.impl.JacksonBodyConverter;
import ly.apps.android.rest.utils.FileUtils;
import ly.apps.android.rest.utils.HeaderUtils;
import org.apache.http.entity.StringEntity;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class JsonBodyConverterTest extends InstrumentationTestCase {

    private BodyConverter converter;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        converter = new JacksonBodyConverter();
    }

    public void testToRequestBody() throws IOException {
        String requestBody = FileUtils.convertStreamToString(
                converter.toRequestBody(SampleBean.testInstance(), HeaderUtils.CONTENT_TYPE_JSON).getContent()
        );
        assertEquals(
                SampleBean.testInstance(),
                converter.fromResponseBody(
                        SampleBean.class,
                        HeaderUtils.CONTENT_TYPE_JSON,
                        new StringEntity(requestBody),
                        new TestCallback<SampleBean>()
                )
        );
    }

    public void testFromResponseBody() throws UnsupportedEncodingException {
        assertEquals(
                SampleBean.testInstance(),
                converter.fromResponseBody(
                        SampleBean.class,
                        HeaderUtils.CONTENT_TYPE_JSON,
                        new StringEntity(SampleBean.testInstanceAsString()),
                        new TestCallback<SampleBean>()
                )
        );
    }

    public void testSupportsRequestContentType() {
        assertTrue(converter.supportsRequestContentType(HeaderUtils.CONTENT_TYPE_JSON));
    }

    public void testSupportsResponseContentType() {
        assertTrue(converter.supportsResponseContentType(HeaderUtils.CONTENT_TYPE_JSON));
    }
}
